/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.JsonModel;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.resource.entity.ResourceCourse;
import com.lczyfz.istep.modules.resource.entity.ResourceCourseCoursesystem;
import com.lczyfz.istep.modules.resource.entity.ResourceCoursesystem;
import com.lczyfz.istep.modules.resource.service.ResourceCourseCoursesystemService;
import com.lczyfz.istep.modules.resource.service.ResourceCourseService;
import com.lczyfz.istep.modules.resource.service.ResourceCoursesystemService;
import com.lczyfz.istep.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程体系Controller
 *
 * @author dh
 * @version 2017-01-16
 */
@Controller
@RequestMapping(value = "${adminPath}/resource/resourceCoursesystem")
public class ResourceCoursesystemController extends BaseController {

    @Autowired
    private ResourceCoursesystemService resourceCoursesystemService;
    @Autowired
    private ResourceCourseService resourceCourseService;

    @Autowired
    private ResourceCourseCoursesystemService resourceCourseCoursesystemService;
    @Autowired
    private SystemService systemService;
    @ModelAttribute
    public ResourceCoursesystem get(@RequestParam(required = false) String id) {
        ResourceCoursesystem entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = resourceCoursesystemService.get(id);
        }
        if (entity == null) {
            entity = new ResourceCoursesystem();
        }
        return entity;
    }

    @RequiresPermissions("resource:resourceCoursesystem:view")
    @RequestMapping(value = {"list", ""})
    public String list(ResourceCoursesystem resourceCoursesystem, HttpServletRequest request, HttpServletResponse response, Model model) {
        if(systemService.isSuperAdmin()) {
            model.addAttribute("isSuperAdmin", "true");
        }
        else{
            model.addAttribute("isSuperAdmin", "false");
        }
        Page<ResourceCoursesystem> page = resourceCoursesystemService.findPage(new Page<ResourceCoursesystem>(request, response), resourceCoursesystem);
        model.addAttribute("page", page);
        return "modules/resource/resourceCoursesystemList";
    }

    @RequiresPermissions("resource:resourceCoursesystem:view")
    @RequestMapping(value = "form")
    public String form(ResourceCoursesystem resourceCoursesystem, Model model, String type) {
        ResourceCourse resourceCourse = new ResourceCourse();
        List<ResourceCourse> resourceCourseList = resourceCourseService.findList(resourceCourse);
        if (resourceCoursesystem != null && resourceCoursesystem.getId() != null && !resourceCoursesystem.getId().equals("")) {
            logger.debug("课程体系id [{}]", resourceCoursesystem.getId());
            ResourceCourseCoursesystem resourceCourseCoursesystem = new ResourceCourseCoursesystem();
            resourceCourseCoursesystem.setCoursesystemId(resourceCoursesystem.getId());
            List<ResourceCourseCoursesystem> resourceCourseCoursesystemList = resourceCourseCoursesystemService.findList(resourceCourseCoursesystem);


            for (int i = 0; i < resourceCourseList.size(); i++) {
                if (resourceCourseCoursesystemList.size() == 0) {
                    break;
                } else {
                    for (int j = 0; j < resourceCourseCoursesystemList.size(); j++) {
                        if (resourceCourseList.get(i).getId().equals(resourceCourseCoursesystemList.get(j).getCourseId())) {
                            resourceCourseList.get(i).setIsChoose("true");
                            resourceCourseCoursesystemList.remove(j);
                            break;
                        }
                    }
                }

            }
        }
        if(type!=null&&type.equals("copy"))
        {
            resourceCoursesystem.setCoursesystemName(resourceCoursesystem.getCoursesystemName() + "-副本");
        }
        if(systemService.isSuperAdmin())
        {
            model.addAttribute("isSuperAdmin", "true");
        }else
            model.addAttribute("isSuperAdmin", "false");
        model.addAttribute("type", type);
        model.addAttribute("resourceCourseList", resourceCourseList);
        model.addAttribute("resourceCoursesystem", resourceCoursesystem);
        return "modules/resource/resourceCoursesystemForm";
    }

    @RequiresPermissions("resource:resourceCoursesystem:edit")
    @RequestMapping(value = "save")
    public String save(ResourceCoursesystem resourceCoursesystem, Model model, RedirectAttributes redirectAttributes, String duallist, String type) {
        if (!beanValidator(model, resourceCoursesystem)) {
            return form(resourceCoursesystem, model,type);
        }
        logger.debug("type: [{}]", type);
        if (type != null && type.equals("copy")) {
            logger.debug("复制课程体系！");

            ResourceCoursesystem coursesystem = new ResourceCoursesystem();
            coursesystem.setCoursesystemName(resourceCoursesystem.getCoursesystemName());
            ResourceCoursesystem temp = resourceCoursesystemService.findCoursesystemByName(coursesystem);
//            List<ResourceCoursesystem> temp = resourceCoursesystemService.findList(coursesystem);
//            logger.debug("temp.size()===" + temp.size());

            if (temp != null&& temp.getCoursesystemName().equals(resourceCoursesystem.getCoursesystemName())) {
                addMessage(redirectAttributes, "复制课程体系'" + resourceCoursesystem.getCoursesystemName() + "'失败，该课程体系已存在");
            } else {
                if (duallist == null || duallist.equals("")) {
                    addMessage(redirectAttributes, "复制课程体系失败，请选择需要的课程");
                } else {
                    ResourceCoursesystem coursesystem1=new ResourceCoursesystem();
                    coursesystem1.setCoursesystemName(resourceCoursesystem.getCoursesystemName());
                    coursesystem1.setCoursesystemAttribute(resourceCoursesystem.getCoursesystemAttribute());
                    coursesystem1.setRemarks(resourceCoursesystem.getRemarks());
                    resourceCoursesystemService.save(coursesystem1);
                    resourceCourseCoursesystemService.save(duallist, coursesystem1);
                    logger.debug("duallist===！"+duallist);

                    addMessage(redirectAttributes, "复制课程体系'" + resourceCoursesystem.getCoursesystemName() + "'成功");
                }
            }
        }else{
            if (resourceCoursesystemService.get(resourceCoursesystem) == null) {
                logger.debug("新增===");

                ResourceCoursesystem coursesystem = new ResourceCoursesystem();
                coursesystem.setCoursesystemName(resourceCoursesystem.getCoursesystemName());
//                List<ResourceCoursesystem> temp = resourceCoursesystemService.findList(coursesystem);
//                logger.debug("temp.size()===" + temp.size());
                ResourceCoursesystem temp = resourceCoursesystemService.findCoursesystemByName(coursesystem);

                if (temp != null&& temp.getCoursesystemName().equals(resourceCoursesystem.getCoursesystemName())) {
                    addMessage(redirectAttributes, "新增课程体系'" + resourceCoursesystem.getCoursesystemName() + "'失败，该课程体系已存在");
                } else {
                    if (duallist == null || duallist.equals("")) {
                        addMessage(redirectAttributes, "新增课程体系失败，请选择需要的课程");
                    } else {
                        resourceCoursesystemService.save(resourceCoursesystem);
                        resourceCourseCoursesystemService.save(duallist, resourceCoursesystem);
                        addMessage(redirectAttributes, "新增课程体系'" + resourceCoursesystem.getCoursesystemName() + "'成功");
                    }
                }
            } else {
                logger.debug("修改===");
                ResourceCoursesystem temp = resourceCoursesystemService.findCoursesystemByName(resourceCoursesystem);
                if(temp!=null&&!temp.getId().equals(resourceCoursesystem.getId()))
                {
                    addMessage(redirectAttributes,"修改课程体系失败，该课程体系已存在");
                    return "redirect:" + Global.getAdminPath() + "/resource/resourceCoursesystem/?repage";

                }

                if (duallist == null || duallist.equals("")) {
                    addMessage(redirectAttributes, "修改课程体系失败，请选择需要的课程");
                } else {
                    resourceCoursesystemService.save(resourceCoursesystem);
                    resourceCourseCoursesystemService.update(duallist, resourceCoursesystem);
                    addMessage(redirectAttributes, "修改课件'" + resourceCoursesystem.getCoursesystemName() + "'成功");
                }

            }
        }




        return "redirect:" + Global.getAdminPath() + "/resource/resourceCoursesystem/?repage";
    }

    @RequiresPermissions("resource:resourceCoursesystem:edit")
    @RequestMapping(value = "delete")
    public String delete(ResourceCoursesystem resourceCoursesystem, RedirectAttributes redirectAttributes) {
        resourceCoursesystemService.delete(resourceCoursesystem);
        addMessage(redirectAttributes, "删除课程体系成功");
        return "redirect:" + Global.getAdminPath() + "/resource/resourceCoursesystem/?repage";
    }

    //根据课程体系属性获取课程
    @ResponseBody
    @RequestMapping(value = "findCourseListByAttribute")
    public JsonModel findCourseListByAttribute(@RequestParam(required = false) String attribute) {
        logger.debug("==dingh==" + attribute);

        JsonModel jsonModel = new JsonModel();
        ResourceCourse resourceCourse=new ResourceCourse();
        resourceCourse.setCourseAttribute(attribute);
        List<ResourceCourse> resourceCourseList=resourceCourseService.findList(resourceCourse);
        logger.debug("==dingh==" + resourceCourseList.size());
        jsonModel.setDataObj(resourceCourseList);
        return jsonModel;
    }

    /**
     * 查看课程体系详情
     * @param resourceCoursesystem
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("resource:resourceCourse:view")
    @RequestMapping(value = "detail")
    public String detail(ResourceCoursesystem resourceCoursesystem, Model model, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("resourceCoursesystem=== [{}]", resourceCoursesystem.getId());
        ResourceCourse resourceCourse=new ResourceCourse();
        resourceCourse.setCourseSystemId(resourceCoursesystem.getId());
        Page<ResourceCourse> page = resourceCourseService.findPageByCoursesystemId(new Page<ResourceCourse>(request, response), resourceCourse);
        logger.debug("page.getList().size()=== [{}]", page.getList().size());

        model.addAttribute("page", page);
        model.addAttribute("resourceCoursesystem", resourceCoursesystem);
        return "modules/resource/resourceCoursesystemDetail";
    }

    /**
     * 根据培训方案id查询课程体系列表展示
     * @param resourceCoursesystem
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("resource:resourceCourseware:view")
    @RequestMapping(value = "findPageByTrainprogramId")
    public String findPageByTrainprogramId(ResourceCoursesystem resourceCoursesystem, HttpServletRequest request, HttpServletResponse response, Model model) {
        logger.debug("courseId=== [{}]", resourceCoursesystem.getTrainprogramId());
        Page<ResourceCoursesystem> page = resourceCoursesystemService.findPageByTrainprogramId(new Page<ResourceCoursesystem>(request, response), resourceCoursesystem);
        logger.debug("page.getList().size()=== [{}]", page.getList().size());
        model.addAttribute("page", page);
        return "modules/resource/resourceCoursesystemList";
    }

}